package cn.hy.preparelesson.common.service;


import cn.hy.pms.thrift.utils.ThriftUtils;
import cn.hy.preparelesson.common.entity.*;
import cn.hy.preparelesson.common.enums.DataStatus;
import cn.hy.preparelesson.common.exception.MessageException;
import cn.hy.preparelesson.common.mapper.PlFeedbackMapper;
import cn.hy.preparelesson.common.params.PageParams;
import cn.hy.preparelesson.common.utils.IdUtils;
import cn.hy.preparelesson.common.utils.PageInfoUtils;
import cn.hy.preparelesson.common.utils.UserUtils;
import cn.hy.preparelesson.common.vo.PageInfo;
import cn.hy.preparelesson.common.vo.PersonalFeedbackVo;
import cn.hy.preparelesson.common.vo.PlFeedbackSaveVo;
import cn.hy.preparelesson.common.vo.SysUserIdAndNameVo;
import com.alibaba.druid.sql.PagerUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.thrift.TException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 反馈Service
 */

@Service
public class PlFeedBackService extends  BaseService {

    @Autowired
    private PlFeedbackMapper feedbackMapper;


    @Autowired
    private PlFeedBackLessonDetailService plFeedBackLessonDetailService;

    @Autowired
    private  PLFeedBackUserDetailService plFeedBackUserDetailService;

    @Autowired
    private PlLessonService plLessonService;
    /**
     * 保存反馈信息
     * @param schoolId
     * @param opeUserId
     * @param plFeedbackSaveVo
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(String schoolId, String opeUserId, PlFeedbackSaveVo plFeedbackSaveVo, List<String> lessonIdList) {
        Date now  = new Date();
        PlFeedbackWithBLOBs plFeedback = new PlFeedbackWithBLOBs();
        BeanUtils.copyProperties(plFeedbackSaveVo, plFeedback);
        String feedbackId = IdUtils.newId();
        plFeedback.setId(feedbackId);
        plFeedback.setSchoolId(schoolId);
        plFeedback.setStatus(DataStatus.NORMAL.getCode());
        plFeedback.setCreateAt(now);
        plFeedback.setCreateBy(opeUserId);
        plFeedback.setUpdateAt(now);
        plFeedback.setUpdateBy(opeUserId);
        feedbackMapper.insert(plFeedback);

        if (!CollectionUtils.isEmpty(lessonIdList)) {
            // 保存 反馈-备课 关联 关系
            plFeedBackLessonDetailService.batchSaveFBLD(schoolId, opeUserId, plFeedback.getId(), lessonIdList, now);
        }
        List<String> toUserIdList = Arrays.asList(plFeedbackSaveVo.getFbToUserIdString().split(",")).stream().map(s -> (s.trim())).collect(Collectors.toList());
        //保存备课-反馈用户 关联关系
        plFeedBackUserDetailService.batchSavePFUD(schoolId, opeUserId, plFeedback.getId(), toUserIdList, now);
    }

    /**
     * 修改反馈
     * @param schoolId
     * @param opeUserId
     * @param plFeedbackSaveVo
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(String schoolId, String opeUserId, PlFeedbackSaveVo plFeedbackSaveVo, List<String> lessonIdList) {
        PlFeedbackWithBLOBs curplFeedback = findValidFeedbackBy(schoolId, opeUserId, plFeedbackSaveVo.getId());
        if (curplFeedback == null) {
            throw new MessageException("当前反馈不存在!");
        }
        if (!curplFeedback.getCreateBy().equals(UserUtils.getUserId())) {
            throw new MessageException("您只能编辑自己创建的反馈的信息!");
        }
        curplFeedback.setTitle(plFeedbackSaveVo.getTitle());
        curplFeedback.setCheckContent(plFeedbackSaveVo.getCheckContent());
        curplFeedback.setSubjectId(plFeedbackSaveVo.getSubjectId());
        curplFeedback.setSubjectName(plFeedbackSaveVo.getSubjectName());
        curplFeedback.setSubjectType(plFeedbackSaveVo.getSubjectType());
        curplFeedback.setSubjectCode(plFeedbackSaveVo.getSubjectCode());
        curplFeedback.setSubjectLevel(plFeedbackSaveVo.getSubjectLevel());
        curplFeedback.setDistanceYear(plFeedbackSaveVo.getDistanceYear());
        curplFeedback.setGradeName(plFeedbackSaveVo.getGradeName());
        curplFeedback.setOverallSituation(plFeedbackSaveVo.getOverallSituation());
        curplFeedback.setRepairSuggession(plFeedbackSaveVo.getRepairSuggession());
        curplFeedback.setUpdateAt(new Date());
        feedbackMapper.updateByPrimaryKeyWithBLOBs(curplFeedback);

        // 修改 反馈-备课关联关系
        plFeedBackLessonDetailService.updateFBLDBy(schoolId, opeUserId, curplFeedback.getId(), lessonIdList, new Date());
        //修改  反馈-反馈给谁关联关系
        List<String> toUserIdList = Arrays.asList(plFeedbackSaveVo.getFbToUserIdString().split(",")).stream().map(s -> (s.trim())).collect(Collectors.toList());
        plFeedBackUserDetailService.updateFBLUDBy(schoolId, opeUserId, curplFeedback.getId(), toUserIdList, new Date());
    }

    /**
     * 查询反馈详情
     * @param schoolId
     * @param opeUserId
     * @param feedbackId
     * @return
     */
    public PlFeedbackSaveVo findValidByFeedbackId(String schoolId, String opeUserId, String feedbackId) throws TException {
        PlFeedbackWithBLOBs curFeedback = findValidFeedbackBy(schoolId, opeUserId, feedbackId);
        if (curFeedback == null) {
            throw  new MessageException("当前反馈不存在或已被删除！");
        }
        PlFeedbackSaveVo plFeedbackSaveVo = new PlFeedbackSaveVo();
        BeanUtils.copyProperties(curFeedback, plFeedbackSaveVo);

        // 备课-反馈 关联集合
        List<PlFeedbackLessonDetail> plFeedbackLessonDetailList = plFeedBackLessonDetailService.findListBySchoolIdAndFeedbackId(schoolId, feedbackId);
        if (!CollectionUtils.isEmpty(plFeedbackLessonDetailList)) {
            List<String> lessonIdList = plFeedbackLessonDetailList.stream().map(PlFeedbackLessonDetail::getLessonId).collect(Collectors.toList());
            plFeedbackSaveVo.setLessonString(String.join(",", lessonIdList));
        }

        List<PlFeedbackUserDetail> plFeedbackUserDetailList = plFeedBackUserDetailService.findListByFeedbackId(schoolId, feedbackId);
        if (!CollectionUtils.isEmpty(plFeedbackUserDetailList)) {
            List<String> userIdList = plFeedbackUserDetailList.stream().map(PlFeedbackUserDetail::getUserId).collect(Collectors.toList());
            List<SysUserIdAndNameVo> userNameAndIdList = ThriftUtils.findUserNameByUserIdList(userIdList);
            if (!CollectionUtils.isEmpty(userNameAndIdList)) {
                List <String> userNameList = new ArrayList<>();
                List <String> fbToUserIdList = plFeedbackUserDetailList.stream().map(PlFeedbackUserDetail::getUserId).collect(Collectors.toList());
                for (String userId : userIdList) {
                    SysUserIdAndNameVo tempUser = userNameAndIdList.stream().filter(user -> user.getUserId().equals(userId)).findFirst().orElse(null);
                    if (tempUser != null) {
                        userNameList.add(tempUser.getName());
                        fbToUserIdList.add(tempUser.getUserId());
                    }
                }
                plFeedbackSaveVo.setFbToUserNameString(String.join(",", userNameList.stream().distinct().collect(Collectors.toList())));
                plFeedbackSaveVo.setFbToUserIdString(String.join(",", fbToUserIdList.stream().distinct().collect(Collectors.toList())));
            }
//            plFeedbackSaveVo.setFbToUserIdString(fbToUserIdList);
        }

//        if (curFeedback.getCreateBy().equals(UserUtils.getUserId())) {
//            plFeedbackSaveVo.setCreateByName(UserUtils.getUsername());
//        } else {
//            List<SysUserIdAndNameVo> userNameByUserIdList = ThriftUtils.findUserNameByUserIdList(Arrays.asList(curFeedback.getCreateBy()));
//            if (CollectionUtils.isEmpty(userNameByUserIdList)){
//                plFeedbackSaveVo.setCreateByName("该账户已注销！");
//            } else{
//                plFeedbackSaveVo.setCreateByName(userNameByUserIdList.get(0).getName());
//            }
//        }
        List<SysUserIdAndNameVo> userNameByUserIdList = ThriftUtils.findUserNameByUserIdList(Arrays.asList(curFeedback.getCreateBy()));
        if (CollectionUtils.isEmpty(userNameByUserIdList)){
            plFeedbackSaveVo.setCreateByName("该账户已注销！");
        } else{
            plFeedbackSaveVo.setCreateByName(userNameByUserIdList.get(0).getName());
        }
        return plFeedbackSaveVo;
    }


    /**
     * 查询反馈详情
     * @param schoolId
     * @param opeUserId
     * @param feedbackId
     * @return
     */
    public PlFeedbackWithBLOBs findValidFeedbackBy(String schoolId, String opeUserId, String feedbackId) {
        PlFeedbackExample example = new PlFeedbackExample();

        PlFeedbackExample.Criteria criteria = example.createCriteria();
        criteria.andSchoolIdEqualTo(schoolId).andStatusEqualTo(DataStatus.NORMAL.getCode());
        if (StringUtils.isNotBlank(opeUserId)) {
            criteria.andCreateByEqualTo(opeUserId);
        }
        if (StringUtils.isNotBlank(feedbackId)) {
            criteria.andIdEqualTo(feedbackId);
        }
        List<PlFeedbackWithBLOBs> plFeedbacks = feedbackMapper.selectByExampleWithBLOBs(example);
        return CollectionUtils.isEmpty(plFeedbacks) ? null : plFeedbacks.get(0);
    }

    /**
     * 分页查询反馈信息
     * @param schoolId
     * @param pageParams
     * @param title
     * @param createStartTime
     * @param createEndTime
     * @param subjectId
     * @param distanceYear
     * @param schoolYear
     * @param term
     * @param userId
     * @return
     */
    public PageInfo<PersonalFeedbackVo> findValidPageBy(String schoolId, PageParams pageParams, String title, Date createStartTime, Date createEndTime, String subjectId, BigDecimal distanceYear, Integer schoolYear, Byte term, String userId) throws TException {
        List<PersonalFeedbackVo> feedbackList = findValidListBy(schoolId, title, createStartTime, createEndTime, subjectId, distanceYear, schoolYear, term, userId);
        return PageInfoUtils.buildPageInfo(feedbackList, pageParams.getPageNum(), pageParams.getPageSize());
    }


    /**
     * 分页查询 反馈 列表信息
     * userId == null 查询全校反馈
     * @param schoolId
     * @param pageParams
     * @param title
     * @param createStartTime
     * @param createEndTime
     * @param subjectId
     * @param distanceYear
     * @param schoolYear
     * @param term
     * @param userId
     * @return
     * @throws TException
     */
    public PageInfo<PlFeedback> findFeedbackListPageBy(String schoolId, PageParams pageParams, String title, Date createStartTime, Date createEndTime, String subjectId, BigDecimal distanceYear, Integer schoolYear, Byte term, String userId) throws TException {
        PageHelper.startPage(pageParams);
        PageInfo<PlFeedback> pageInfo = new PageInfo<>(findValidFeedBackListBy(schoolId, title, createStartTime, createEndTime, subjectId, distanceYear, schoolYear, term, userId));
        if (pageInfo.getList() != null && !CollectionUtils.isEmpty(pageInfo.getList())) {
            List<PlFeedback> feedbackList = pageInfo.getList();
            // 反馈Id List
            List<String> feedbackIdList = feedbackList.stream().map(PlFeedback::getId).collect(Collectors.toList());

            //反馈 - 反馈给谁关系关联集合
            List<PlFeedbackUserDetail> feedbackUserList = plFeedBackUserDetailService.findListByFeedbackIdIn(schoolId, feedbackIdList);

            if (!CollectionUtils.isEmpty(feedbackUserList)) {
                //用户id集合
                List<String> userIdList = feedbackUserList.stream().map(PlFeedbackUserDetail::getUserId).collect(Collectors.toList());
                List<String> feedbackCreateByList = feedbackList.stream().map(PlFeedback::getCreateBy).collect(Collectors.toList());
                userIdList.addAll(feedbackCreateByList);
                List<SysUserIdAndNameVo> userNameList = ThriftUtils.findUserNameByUserIdList(userIdList);
                Map<String, List<PlFeedbackUserDetail>> fbUserMap = feedbackUserList.stream().collect(Collectors.groupingBy(PlFeedbackUserDetail::getFeedbackId));
                for (Map.Entry<String, List<PlFeedbackUserDetail>> entry : fbUserMap.entrySet()) {
                    String tempFeedbackId = entry.getKey();
                    List<String> tempUserIdList = entry.getValue().stream().map(PlFeedbackUserDetail::getUserId).collect(Collectors.toList());
                    PlFeedback tempFeedback;
                    tempFeedback = feedbackList.stream().filter(feedback -> feedback.getId().equals(tempFeedbackId)).findFirst().orElse(null);
                    if (tempFeedback != null) {
                        userNameList.stream().filter(user -> tempFeedback.getCreateBy().equals(user.getUserId())).findFirst().ifPresent(createByName -> tempFeedback.setCreateByName(createByName.getName()));
                        List<SysUserIdAndNameVo> tempUserList = userNameList.stream().filter(user -> tempUserIdList.contains(user.getUserId())).collect(Collectors.toList());
                        if (!CollectionUtils.isEmpty(tempUserList)) {
                            List<String> nameList = tempUserList.stream().map(SysUserIdAndNameVo::getName).collect(Collectors.toList());
                            tempFeedback.setFeedbackTo(String.join(",", nameList));
                        }
                    }
                }
            }
        }
        return pageInfo;
    }

    /**
     * 查询反馈信息列表
     * @param schoolId
     * @param title
     * @param createStartTime
     * @param createEndTime
     * @param subjectId
     * @param distanceYear
     * @param schoolYear
     * @param term
     * @param userId
     * @return
     */
    public  List<PlFeedback>   findValidFeedBackListBy(String schoolId, String title, Date createStartTime,  Date createEndTime, String subjectId, BigDecimal distanceYear, Integer schoolYear, Byte term, String userId) {
        PlFeedbackExample example = new PlFeedbackExample();
        PlFeedbackExample.Criteria criteria = example.createCriteria();
        criteria.andSchoolIdEqualTo(schoolId).andStatusEqualTo(DataStatus.NORMAL.getCode());
        if (StringUtils.isNotBlank(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (createStartTime != null) {
            criteria.andCreateAtGreaterThanOrEqualTo(createStartTime);
        }
        if (createEndTime != null) {
            criteria.andCreateAtLessThanOrEqualTo(createEndTime);
        }
        if (StringUtils.isNotBlank(subjectId)) {
            criteria.andSubjectIdEqualTo(subjectId);
        }
        if (distanceYear != null) {
            criteria.andDistanceYearEqualTo(distanceYear);
        }
        if (term != null) {
            criteria.andTermEqualTo(term);
        }
        if (schoolYear != null) {
            criteria.andSchoolYearEqualTo(schoolYear);
        }
        if (userId != null) {
            //反馈 - 反馈给谁关联关系集合
            List<PlFeedbackUserDetail> PLFBUDList = plFeedBackUserDetailService.findListBySchoolIdAndUserId(schoolId, userId);
            if (CollectionUtils.isEmpty(PLFBUDList)) {
                return new ArrayList<>();
            }
            List<String> feedbackIdList = PLFBUDList.stream().map(PlFeedbackUserDetail::getFeedbackId).collect(Collectors.toList());
            criteria.andIdIn(feedbackIdList);
        }
        example.setOrderByClause("create_at desc");
       return feedbackMapper.selectByExample(example);
    }

    /**
     * 查询备课列表
     * @param schoolId
     * @param title
     * @param createStartTime
     * @param createEndTime
     * @param subjectId
     * @param distanceYear
     * @param schoolYear
     * @param term
     * @param userId
     * @return
     */
    public List<PersonalFeedbackVo> findValidListBy(String schoolId, String title, Date createStartTime, Date createEndTime, String subjectId, BigDecimal distanceYear, Integer schoolYear, Byte term, String userId) throws TException {
        PlFeedbackExample example = new PlFeedbackExample();
        PlFeedbackExample.Criteria criteria = example.createCriteria();
        criteria.andSchoolIdEqualTo(schoolId).andStatusEqualTo(DataStatus.NORMAL.getCode());
        if (StringUtils.isNotBlank(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (createStartTime != null) {
            criteria.andCreateAtGreaterThanOrEqualTo(createStartTime);
        }
        if (createEndTime != null) {
            criteria.andCreateAtLessThanOrEqualTo(createEndTime);
        }
        if (StringUtils.isNotBlank(subjectId)) {
            criteria.andSubjectIdEqualTo(subjectId);
        }
        if (distanceYear != null) {
            criteria.andDistanceYearEqualTo(distanceYear);
        }
        if (term != null) {
            criteria.andTermEqualTo(term);
        }
        if (schoolYear != null) {
            criteria.andSchoolYearEqualTo(schoolYear);
        }
        //userId != null 查询出反馈给改userId的 反馈信息
        if (userId != null) {
            //反馈 - 反馈给谁关联关系集合
            List<PlFeedbackUserDetail> PLFBUDList = plFeedBackUserDetailService.findListBySchoolIdAndUserId(schoolId, userId);
            if (CollectionUtils.isEmpty(PLFBUDList)) {
                return new ArrayList<>();
            }
            List<String> feedbackIdList = PLFBUDList.stream().map(PlFeedbackUserDetail::getFeedbackId).collect(Collectors.toList());
            criteria.andIdIn(feedbackIdList);
        }
        example.setOrderByClause("create_at desc");
        List<PlFeedback> feedbackList = feedbackMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(feedbackList)) {
            return new ArrayList<>();
        }

        List<PersonalFeedbackVo> feedbackVoList = new ArrayList<>();

        //反馈Id List
        List<String> feedbackIdList = feedbackList.stream().map(PlFeedback::getId).collect(Collectors.toList());
        //反馈 - 反馈给谁关系关联集合
        List<PlFeedbackUserDetail> feedbackUserList = plFeedBackUserDetailService.findListByFeedbackIdIn(schoolId, feedbackIdList);
        if (CollectionUtils.isEmpty(feedbackUserList)) {
            return new ArrayList<>();
        }
        //用户id集合
        List<String> userIdList = feedbackUserList.stream().map(PlFeedbackUserDetail::getUserId).collect(Collectors.toList());
        List<String> feedbackCreateByList = feedbackList.stream().map(PlFeedback::getCreateBy).collect(Collectors.toList());
        userIdList.addAll(feedbackCreateByList);
        List<SysUserIdAndNameVo> userNameList = ThriftUtils.findUserNameByUserIdList(userIdList);

        //构建反馈 反馈给谁集合
        Map<String, List<PlFeedbackUserDetail>> fbUserMap = feedbackUserList.stream().collect(Collectors.groupingBy(PlFeedbackUserDetail::getFeedbackId));
        for (Map.Entry<String, List<PlFeedbackUserDetail>> entry : fbUserMap.entrySet()) {
            String tempFeedbackId = entry.getKey();
            List<String> tempUserIdList = entry.getValue().stream().map(PlFeedbackUserDetail::getUserId).collect(Collectors.toList());
            PlFeedback tempFeedback = feedbackList.stream().filter(feedback -> feedback.getId().equals(tempFeedbackId)).findFirst().orElse(null);
            if (tempFeedback != null) {
                PersonalFeedbackVo personalFeedbackVo = new PersonalFeedbackVo();
                BeanUtils.copyProperties(tempFeedback, personalFeedbackVo);
                SysUserIdAndNameVo createByName =  userNameList.stream().filter(user -> tempFeedback.getCreateBy().equals(user.getUserId())).findFirst().orElse(null);
               if (createByName != null) {
                   personalFeedbackVo.setCreateByName(createByName.getName());
               }
                List<SysUserIdAndNameVo> tempUserList = userNameList.stream().filter(user -> tempUserIdList.contains(user.getUserId())).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(tempUserList)) {
                    List<String> nameList = tempUserList.stream().map(SysUserIdAndNameVo::getName).collect(Collectors.toList());
                    personalFeedbackVo.setFeedbackTo(String.join(",", nameList));
                }
                feedbackVoList.add(personalFeedbackVo);
            }
        }
        return feedbackVoList.stream().sorted(Comparator.comparing(PersonalFeedbackVo::getCreateAt).reversed()).collect(Collectors.toList());
    }


    /**
     * 获取当前用户的分页反馈数据
     * @param schoolId
     * @param pageParams
     * @param userId
     * @return
     */
    public PageInfo<PersonalFeedbackVo> findPersonalFeedbackPageBy(String schoolId, PageParams pageParams, String title, Date createStartTime,
                                                                   Date createEndTime, String subjectId, BigDecimal distanceYear,Integer schoolYear, Byte term, String userId) throws TException {
        PageHelper.startPage(pageParams.getPageNum(), pageParams.getPageSize());
        PageInfo<PersonalFeedbackVo> pageInfo = new PageInfo<>(findPersonalFeedbackListBy(schoolId, userId));
        return pageInfo;
    }

    /**
     * 根据用户id获取id集合
     * @param schoolId
     * @param userId
     * @return
     */
    public List<PersonalFeedbackVo> findPersonalFeedbackListBy(String schoolId, String userId) throws TException {
       //反馈 - 反馈给谁关联关系集合
        List<PlFeedbackUserDetail> PLFBUDList = plFeedBackUserDetailService.findListBySchoolIdAndUserId(schoolId, userId);
        if (CollectionUtils.isEmpty(PLFBUDList)) {
            return new ArrayList<>();
        }
        List<String> feedbackIdList = PLFBUDList.stream().map(PlFeedbackUserDetail::getFeedbackId).collect(Collectors.toList());
        List<PlFeedbackWithBLOBs> feedbackList = findValidListBySchoolIdAndIdIn(schoolId, feedbackIdList);
        // 备课 - 反馈关联关系集合
        List<PlFeedbackLessonDetail> PLFBLessonDetailList = plFeedBackLessonDetailService.findListBySchoolIdAndFeedbackIdIn(schoolId, feedbackIdList);
        if (CollectionUtils.isEmpty(feedbackList)) {
            return new ArrayList<>();
        }
        // 反馈创建人Id集合
        List<String> feedbackCreateIdList = feedbackList.stream().map(PlFeedbackWithBLOBs::getCreateBy).collect(Collectors.toList());
        List<SysUserIdAndNameVo> fbCreateByNameList = ThriftUtils.findUserNameByUserIdList(feedbackCreateIdList);
        List<PersonalFeedbackVo> personalFBVoList = new ArrayList<>();
        for (PlFeedbackWithBLOBs feedback : feedbackList) {
            PersonalFeedbackVo feedbackVo = new PersonalFeedbackVo();
            BeanUtils.copyProperties(feedback, feedbackVo);
            SysUserIdAndNameVo tempSysUserVo = fbCreateByNameList.stream().filter(sysUser -> feedback.getCreateBy().equals(sysUser.getUserId())).findFirst().orElse(null);
            feedbackVo.setCreateByName(tempSysUserVo.getName());
            personalFBVoList.add(feedbackVo);
        }
        //构建反馈 - 备课关联关系
        if (!CollectionUtils.isEmpty(PLFBLessonDetailList)) {
            List<String> lessonIdList = PLFBLessonDetailList.stream().map(PlFeedbackLessonDetail::getLessonId).collect(Collectors.toList());
            List<PlLesson> plLessonList = plLessonService.findValidListBySchoolIdAndIdIn(schoolId, lessonIdList);
            if (!CollectionUtils.isEmpty(plLessonList)) {
                Map<String, List<PlFeedbackLessonDetail>> PLFBLessonDetailMap = PLFBLessonDetailList.stream().collect(Collectors.groupingBy(PlFeedbackLessonDetail::getFeedbackId));
                for(Map.Entry<String, List<PlFeedbackLessonDetail>> entry : PLFBLessonDetailMap.entrySet()) {
                    String  tempFeedbackId = entry.getKey();
                    PersonalFeedbackVo feedbackVo = personalFBVoList.stream().filter(feedback -> feedback.getId().equals(tempFeedbackId)).findFirst().orElse(null);
                    if (feedbackVo != null) {
                        List<String> tempLessonIdList = entry.getValue().stream().map(PlFeedbackLessonDetail::getLessonId).collect(Collectors.toList());
                        List<PlLesson> filterPlLessonList = plLessonList.stream().filter(plLesson -> tempLessonIdList.contains(plLesson.getId())).collect(Collectors.toList());
                        if (!CollectionUtils.isEmpty(filterPlLessonList)) {
                            List<String> plTitleList = filterPlLessonList.stream().map(PlLesson::getTitle).collect(Collectors.toList());
                            feedbackVo.setAssociationLessonTitle(String.join(",", plTitleList));
                        }
                    }
                }
            }
        }

        //构建反馈 - 反馈给谁
        //反馈给谁Id集合
        List<String> feedbackIds = PLFBUDList.stream().map(PlFeedbackUserDetail::getFeedbackId).distinct().collect(Collectors.toList());
        List<PlFeedbackUserDetail> listByFeedbackIdIn = plFeedBackUserDetailService.findListByFeedbackIdIn(schoolId, feedbackIds);
        List<String> feedbackToUserIdList = listByFeedbackIdIn.stream().map(PlFeedbackUserDetail::getUserId).collect(Collectors.toList());
        Map<String, List<PlFeedbackUserDetail>> feedbackUserDetailMap = listByFeedbackIdIn.stream().collect(Collectors.groupingBy(PlFeedbackUserDetail::getFeedbackId));
        List<SysUserIdAndNameVo> sysUserNameList = ThriftUtils.findUserNameByUserIdList(feedbackToUserIdList);
        if(!CollectionUtils.isEmpty(sysUserNameList)) {
            for(Map.Entry<String, List<PlFeedbackUserDetail>> entry : feedbackUserDetailMap.entrySet()) {
                String tempFeedbackId = entry.getKey();
                PersonalFeedbackVo feedbackVo = personalFBVoList.stream().filter(feedback -> feedback.getId().equals(tempFeedbackId)).findFirst().orElse(null);
                if (feedbackVo != null) {
                    List<String> tempUserIdList = entry.getValue().stream().map(PlFeedbackUserDetail::getUserId).collect(Collectors.toList());
                    List<SysUserIdAndNameVo> filterSysNameList = sysUserNameList.stream().filter(sysUser -> tempUserIdList.contains(sysUser.getUserId())).collect(Collectors.toList());
                    if (!CollectionUtils.isEmpty(filterSysNameList)) {
                        List<String> nameList = filterSysNameList.stream().map(SysUserIdAndNameVo::getName).collect(Collectors.toList());
                        feedbackVo.setFeedbackTo(String.join(",", nameList));
                    }
                }
            }
        }
        return personalFBVoList;
    }

    /**
     * 根据schoolId, idList 查询反馈集合
     * @param schoolId
     * @param feedbackIdList
     * @return
     */
    public List<PlFeedbackWithBLOBs> findValidListBySchoolIdAndIdIn(String schoolId, List<String> feedbackIdList) {
        PlFeedbackExample example = new PlFeedbackExample();
        example.createCriteria().andStatusEqualTo(DataStatus.NORMAL.getCode()).andSchoolIdEqualTo(schoolId).andIdIn(feedbackIdList);
       return feedbackMapper.selectByExampleWithBLOBs(example);
    }

    /**
     *软删除反馈
     * @param schoolId
     * @param userId
     * @param feedbackId
     */
    @Transactional(rollbackFor = Exception.class)
    public void softDelete(String schoolId, String userId, String feedbackId) {
        PlFeedbackWithBLOBs feedbackCheck = findValidFeedbackBy(schoolId, null, feedbackId);
        if (feedbackCheck == null) {
            throw new MessageException("当前记录不存在！");
        }
        if (!UserUtils.getUserId().equals(feedbackCheck.getCreateBy())) {
            throw new MessageException("您只能删除自己发布的反馈！");
        }
        PlFeedbackWithBLOBs feedbackForUpdate = new PlFeedbackSaveVo();
        feedbackForUpdate.setId(feedbackId);
        feedbackForUpdate.setUpdateAt(new Date());
        feedbackForUpdate.setUpdateBy(userId);
        feedbackForUpdate.setStatus(DataStatus.DELETE.getCode());
        feedbackMapper.updateByPrimaryKeySelective(feedbackForUpdate);

        plFeedBackLessonDetailService.batchDeleteFBLD(schoolId, feedbackId);
        plFeedBackUserDetailService.batchDeleteFBUD(schoolId, feedbackId);
    }
}
